
package guoan.web.webCustomer;

import guoan.core.common.WebConfig;
import guoan.core.dto.DataGridModel;
import guoan.core.dto.PageResponse;
import guoan.core.dto.RestResponse;
import guoan.domain.CoachConfig;
import guoan.domain.Exam;
import guoan.domain.ExamConfig;
import guoan.service.ExamConfigService;
import guoan.service.ExamService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Preconditions;

@Controller
@RequestMapping("/webCustomer/exam")
public class ExamController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ExamService examService;
    @Autowired
    private ExamConfigService examConfigService;

    /** 跳转到考试主页 */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(){
    	examService.removeLast();
        return "/webCustomer/exam/index";
    }

    /** 跳转到添加预约考试 */
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String toAdd(Model model){
        // 写入预约考试默认的
        model.addAttribute("examRanges", WebConfig.EXAM_OPENHOURS_RANGE);
        return "/webCustomer/exam/add";
    }

    /** 保存预约考试 */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> add(Exam exam){
        Preconditions.checkNotNull(exam, "请填写预约考试");
        return examService.add(exam);
    }

    /** 保存预约考试 */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> update(Exam exam){
        Preconditions.checkNotNull(exam, "请填写预约考试");
        return examService.update(exam);
    }

    @RequestMapping(value = "data", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<Exam> data(DataGridModel dataGridModel, @RequestParam(
            defaultValue = "ALL") String type){
        return examService.page(type, dataGridModel);
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> del(Long id){
        Preconditions.checkNotNull(id, "参数错误");
        examService.delete(id);
        return RestResponse.ok(true);
    }

    @RequestMapping(value = "examConfig", method = RequestMethod.GET)
    public String coachConfig(Model model){
        ExamConfig cc = examConfigService.findExamConfig();
        model.addAttribute("cc", cc);
        return "/webCustomer/exam/examConfig";
    }

    @RequestMapping(value = "examConfig", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> update(ExamConfig examConfig){
        Preconditions.checkNotNull(examConfig, "参数错误");
        return examConfigService.update(examConfig);
    }
}
